package com.wazzapps.sdk;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.RemoteException;
import android.util.Log;

import com.android.installreferrer.api.InstallReferrerClient;
import com.android.installreferrer.api.InstallReferrerStateListener;

public class WazzInstallReceiver {
    public static final String REFERRER_KEY = "refferer";

    public static void getReferrerFromApi(final Context context, final Runnable runnable) {
        final InstallReferrerClient build = InstallReferrerClient.newBuilder(context).build();
        build.startConnection(new InstallReferrerStateListener() { // from class: com.wazzapps.sdk.WazzInstallReceiver.1
            @Override // com.android.installreferrer.api.InstallReferrerStateListener
            public void onInstallReferrerServiceDisconnected() {
            }

            @Override // com.android.installreferrer.api.InstallReferrerStateListener
            public void onInstallReferrerSetupFinished(int i) {
                if (i == 0) {
                    try {
                        String installReferrer = build.getInstallReferrer().getInstallReferrer();
                        Log.d("WazzSdk", "Reveived referrer from API: " + installReferrer);
                        WazzInstallReceiver.writeReferrer(context, installReferrer);
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
                }
                try {
                    build.endConnection();
                } catch (IllegalArgumentException e2) {
                    e2.printStackTrace();
                }
                runnable.run();
            }
        });
    }

    /* access modifiers changed from: private */
    public static void writeReferrer(Context context, String str) {
        SharedPreferences.Editor edit = context.getSharedPreferences(WazzApplicationInstance.USER_PREFS, 0).edit();
        edit.putString(REFERRER_KEY, str);
        edit.commit();
    }
}